/*
 * @Description: 
 * @Author: Hongyang_Yang
 * @Date: 2020-07-24 14:23:56
 * @LastEditors: Hongyang_Yang
 * @LastEditTime: 2020-07-24 14:28:40
 */
/*
 * @lc app=leetcode.cn id=599 lang=cpp
 *
 * [599] 两个列表的最小索引总和
 */

// @lc code=start
class Solution
{
public:
    vector<string> findRestaurant(vector<string> &list1, vector<string> &list2)
    {
        vector<string> res;
        unordered_map<string, int> m;
        int min = INT_MAX;
        for (int i = 0; i < list1.size(); i++)
        {
            m[list1[i]] = i;
        }
        for (int i = 0; i < list2.size(); i++)
        {
            if (m.count(list2[i]) != 0)
            {
                if (m[list2[i]] + i < min)
                {
                    min = m[list2[i]] + i;
                    res.clear();
                    res.push_back(list2[i]);
                }
                else if (m[list2[i]] + i == min)
                {
                    res.push_back(list2[i]);
                }
            }
        }
        return res;
    }
};
// @lc code=end
